package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.CrmDetial;
import com.jianyun.jyzs.bean.CrmSearchBean;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.fragment.BussinessTripFragment;
import com.jianyun.jyzs.fragment.CustomerDetialFragment;
import com.jianyun.jyzs.fragment.ProjectTrackingFragment;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.widget.RedPiontTextView;
import com.jrmf360.rylib.adapter.TradeDetailAdapter;
import com.jrmf360.rylib.common.util.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by yangshijun on 2018/6/4.
 */

public class CrmDetialActivity extends AppCompatActivity {
    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.iv_create)
    ImageView ivCreate;
    @BindView(R.id.pt_1)
    RedPiontTextView pt1;
    @BindView(R.id.pt_2)
    RedPiontTextView pt2;
    @BindView(R.id.pt_3)
    RedPiontTextView pt3;
    @BindView(R.id.id_switch_tab_ll)
    LinearLayout idSwitchTabLl;
    @BindView(R.id.id_tab_line_iv)
    ImageView idTabLineIv;
    @BindView(R.id.viewPager)
    ViewPager viewPager;


    private int lineWidth;
    private List<Fragment> mFragmentList = new ArrayList();
    private TradeDetailAdapter adapter;
    private int currIndex;
    private CrmSearchBean.ListCrm sdata;
    private ProjectTrackingFragment projectTrackingFragment;
    private CustomerDetialFragment customerDetialFragment;
    private BussinessTripFragment bussinessTripFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_crm_detial_layout);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initData() {
        sdata = (CrmSearchBean.ListCrm) getIntent().getSerializableExtra(SysConstant.msg_key_parcelable);
        RceLog.d("lml", sdata.toString());
        title.setVisibility(View.VISIBLE);
        title.setText(sdata.getCustomername());
    }

    private void initView() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(SysConstant.msg_key_parcelable, sdata);
        bussinessTripFragment = new BussinessTripFragment();
        projectTrackingFragment = new ProjectTrackingFragment();
        customerDetialFragment = new CustomerDetialFragment();
        customerDetialFragment.setArguments(bundle);
        bussinessTripFragment.setArguments(bundle);
        projectTrackingFragment.setArguments(bundle);
        mFragmentList.add(bussinessTripFragment);
        mFragmentList.add(projectTrackingFragment);
        mFragmentList.add(customerDetialFragment);
        adapter = new TradeDetailAdapter(getSupportFragmentManager(), mFragmentList);
        viewPager.setAdapter(adapter);
        initTabLineWidth();
        initEvent();
    }


    private void resetTextView() {
        pt1.setTextColor(this.getResources().getColor(R.color.bacck333));
        pt2.setTextColor(this.getResources().getColor(R.color.bacck333));
        pt3.setTextColor(this.getResources().getColor(R.color.bacck333));
    }

    private void initTabLineWidth() {
        pt1.setTextColor(getResources().getColor(R.color.title_bar_color));
        int var1 = ScreenUtil.getScreenWidth(this);
        RelativeLayout.LayoutParams var2 = (RelativeLayout.LayoutParams) idTabLineIv.getLayoutParams();
        lineWidth = var2.width = var1 / this.mFragmentList.size();
        idTabLineIv.setLayoutParams(var2);
    }

    @OnClick({R.id.topBack, R.id.iv_create, R.id.pt_1, R.id.pt_2, R.id.pt_3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBack:
                finish();
                break;
            case R.id.pt_1:
                viewPager.setCurrentItem(0);
                ivCreate.setVisibility(View.VISIBLE);
                break;
            case R.id.pt_2:
                viewPager.setCurrentItem(1);
                ivCreate.setVisibility(View.INVISIBLE);
                break;
            case R.id.pt_3:
                viewPager.setCurrentItem(2);
                ivCreate.setVisibility(View.INVISIBLE);
                break;
            case R.id.iv_create:
                startActivity(new Intent(this, CrmSignCalendarActivity.class).putExtra(SysConstant.msg_key_string1, sdata.getAutoid()+""));
                break;
        }
    }

    private void initEvent() {

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int var1, float var2, int var3) {
                RelativeLayout.LayoutParams var4 = (RelativeLayout.LayoutParams) idTabLineIv.getLayoutParams();
                int var5 = lineWidth * var1 + var3 / mFragmentList.size();
                var4.leftMargin = var5;
                idTabLineIv.setLayoutParams(var4);
            }

            @Override
            public void onPageSelected(int position) {
                resetTextView();
                currIndex = position;
                switch (position) {
                    case 0:
                        pt1.setTextColor(getResources().getColor(R.color.tab_title_wo));
                        ivCreate.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        pt2.setTextColor(getResources().getColor(R.color.tab_title_wo));
                        ivCreate.setVisibility(View.INVISIBLE);
                        projectTrackingFragment.onResume();
                        break;
                    case 2:
                        pt3.setTextColor(getResources().getColor(R.color.tab_title_wo));
                        ivCreate.setVisibility(View.INVISIBLE);
                        customerDetialFragment.onResume();
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
